package starlingui.component
{
	import com.imcrl.component.PageList;
	import com.imcrl.component.ext.ButtonImage;
	import com.imcrl.component.page.Page;
	import com.imcrl.component.pictab.PictabItem;
	import com.imcrl.component.pictab.PictabQ;
	import com.imcrl.core.ClassFactory;
	import com.imcrl.core.IResizeable;
	import com.imcrl.core.imcrl_internal;
	import com.imcrl.core.state.StateMachine;
	import com.imcrl.event.ListItemEvent;
	import com.imcrl.layout.TileLayout;
	
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.events.Event;
	import starling.events.TouchEvent;
	import starling.textures.Texture;
	
	import starlingui.component.itemRender.IconItemRender;
	import starlingui.component.itemRender.PointItemRender;
	import starlingui.manager.TextureLoader;
	import starlingui.manager.TextureResource;
	import starlingui.state.desktopState.AbsState;
	import starlingui.state.desktopState.DefaultState;
	import starlingui.state.desktopState.FolderState;
	import starlingui.state.desktopState.IFolderAction;
	import starlingui.state.desktopState.ShakeState;
	import starlingui.vo.IconVO;
	
	use namespace imcrl_internal;
	public class Desktop extends PictabQ implements IResizeable
	{
		public var openApplicationHandle:Function;
		protected var _bmd:BitmapData;
		
		imcrl_internal var pointList:PageList;
		
		imcrl_internal var _cache_top:Image;	
		imcrl_internal var _cache_botton:Image;
		imcrl_internal var folderList:FolderList;
		
		private var expadHeight:int=250;
		private var expadTime:Number=.3;
		
		private var stateMachine:StateMachine;
		public function Desktop(factory:ClassFactory)
		{	
			
			super(factory);
			
			folderList=new FolderList();
			
			stateMachine=new StateMachine();
			stateMachine.add(new DefaultState(this));
			stateMachine.add(new ShakeState(this));
			stateMachine.add(new FolderState(this));
			
			scrollEnabeld=false;
			addEventListener(ListItemEvent.CHANGE,desktopChangeHandle);
			
			pointList=new PageList(new ClassFactory(PointItemRender));
			pointList.layout=new TileLayout(20,20,10);
			
			this.addChild(pointList);
		}
		
		private function desktopChangeHandle(event:ListItemEvent):void{
			state.end();
			state.start();
			
			pointList.selectedIndex=this.currentIndex;
		}
		
		
		public function get state():AbsState{
			return stateMachine.getState(stateMachine.state) as AbsState;
		}
		
		public function setState(value:String):void{
			stateMachine.state=value;
		}
		
		override protected function backgroundHandle(event:Event):void{
			var textLoader:TextureLoader=event.target as TextureLoader;
			_bmd=textLoader.content;
			super.backgroundHandle(event);
		}
		
		override protected function initBbackgroundTexture(value:Texture):void{
			
			if(_background==null){
				_background=new ButtonImage(value);
				_cache_top=new ButtonImage(value);
				_cache_botton=new ButtonImage(value);
				this.addChildAt(_background,0);
			}else{
				_background.texture=value;
				_cache_top.texture=value;
				_cache_botton.texture=value;
				
				_background.readjustSize();
				_cache_top.readjustSize();
				_cache_botton.readjustSize();
			}
			
			setState(DefaultState.NAME);
		}
		
		
		public function shake():void{
			setState(ShakeState.NAME);
		}
		
		public function unShake():void{
			
			setState(DefaultState.NAME);
		}
		/*public function unExpandFolder(event:Event):void{
		var innerList:DeskList=current as DeskList;
		innerList.layout.unExpand();
		}*/
		
		
		
		private var currentClickY:int;
		private var selectFolderItem:IconItemRender;
		private var rect:Rectangle=new Rectangle();
		private var topTween:Tween=new Tween(null,0);
		private var bottonTween:Tween=new Tween(null,0);
		public function selectedItem(item:IconItemRender,list:DeskList):void{
			if(selectFolderItem){
				return;
			}
			var vo:IconVO=item.data;
			if(vo.sub.length==0){
				if(openApplicationHandle !=null )openApplicationHandle(vo);
				return;
			}
			
			var mx:int=item.x;
			var my:int=item.y+(item.height>>1)+20;
			
			if(_bmd.rect.contains(mx,my)==false || my==0){
				my=Math.max(1,Math.min(my,_bmd.rect.height-1));
			}
			currentClickY=my;
			selectFolderItem=item;
			
			//var time:int=getTimer();
			trace("show");
			rect.setEmpty();
			rect.width=_bmd.width;
			rect.height=my;
			
			var empty:Point=new Point();
			var bmd:BitmapData=new BitmapData(rect.width,rect.height,true,0);
			bmd.copyPixels(_bmd,rect,empty);
			
			_cache_top.y=0;
			_cache_top.texture=Texture.fromBitmapData(bmd,false);
			_cache_top.readjustSize();
			
			
			rect.y=my;
			rect.height=_bmd.height-my;
			bmd=new BitmapData(rect.width,rect.height,true,0);
			bmd.copyPixels(_bmd,rect,empty);
			
			_cache_botton.y=my;
			_cache_botton.texture=Texture.fromBitmapData(bmd,false);
			_cache_botton.readjustSize();
			
			folderList.y=my;
			
			expadHeight=folderList.displayList(vo.sub);
			
			var bottonPos:int=expadHeight;
			
			var maxBottonPos:int=_bmd.height-100;
			var _currentExpadHeight:int=bottonPos;
			
			if(bottonPos+my>maxBottonPos){
				var topPos:int=maxBottonPos-bottonPos-my;
				topTween.reset(_cache_top,expadTime);
				topTween.onUpdate=topOnUpdate;
				topTween.animate("y",topPos);
				Starling.juggler.add(topTween);
				
				bottonPos=maxBottonPos-my;
			}
			
			
			
			folderList.show(_bmd.width,expadHeight,mx);
			
			
			bottonTween.reset(_cache_botton,expadTime);
			bottonTween.animate("y",my+bottonPos);
			Starling.juggler.add(bottonTween);
			
			this.addChildAt(_cache_top,0);
			this.addChildAt(folderList,1);
			this.addChildAt(_cache_botton,2);
			
			this.removeChild(_background);
			
			if(state is IFolderAction){
				IFolderAction(state).expandBy(item,topPos,bottonPos,0.1,false);
			}else{
				setState(FolderState.NAME);
				IFolderAction(state).expandBy(item,topPos,bottonPos,0.1,false);
			}
		}
		private function topOnUpdate():void{
			folderList.y=_cache_top.y+_cache_top.height;
		}
		
		
		public function hideFolder():void{
			trace("hide");
			bottonTween.reset(_cache_botton,expadTime);
			bottonTween.addEventListener(Event.REMOVE_FROM_JUGGLER, hideComplete);
			
			bottonTween.animate("y",currentClickY);
			Starling.juggler.add(bottonTween);
			
			topTween.reset(_cache_top,expadTime);
			topTween.onUpdate=topOnUpdate;
			topTween.animate("y",0);
			Starling.juggler.add(topTween);
			
			var innerList:DeskList=current as DeskList;
			
			if(state is IFolderAction){
				IFolderAction(state).expandBy(selectFolderItem,0,0,1,true);
			}
			_cache_top.touchable=false;
			_cache_botton.touchable=false;
		}
		
		private function hideComplete(event:Event):void{
			event.target.removeEventListener(Event.REMOVE_FROM_JUGGLER, hideComplete);
			trace("hideComp")
			selectFolderItem=null;
			
			var innerList:DeskList=current as DeskList;
			innerList.selectedIndex=-1;
			
			_cache_top.touchable=true;
			_cache_botton.touchable=true;
			
			this.removeChild(_cache_top);
			this.removeChild(_cache_botton);
			this.removeChild(folderList);
			
			this.addChildAt(_background,0);
			
			if(state is IFolderAction){
				var nextState:String=(state as IFolderAction).folderEndState;
				if(nextState != state.name)setState(nextState);
			}else{
				setState(DefaultState.NAME);
			}
		}
		
		override public function displayList(value:*):void{
			super.displayList(value);
			
			list.length=value.length;
			pointList.displayList(list);
			pointList.addEventListener(Event.COMPLETE,pointCompleteHandle);
		}
		
		private var list:Vector.<Object>=new Vector.<Object>();
		private function pointCompleteHandle(event:Event):void{
			pointList.removeEventListener(Event.COMPLETE,pointCompleteHandle);
			
			pointList.y=scrollRect.height-pointList.height;
			pointList.x=(scrollRect.width-pointList.width)>>1;
			pointList.selectedIndex=0;
		}
		
		
		override public function getItemData(index:int):*{
			return _data;
		}
		
		public function get folderIsOpened():Boolean{
			return selectFolderItem !=null;
		}
		
	}
}